﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.ComponentModel;
using Microsoft.WindowsPhone.ChineseInput.InputProviders;

namespace Microsoft.WindowsPhone.ChineseInput
{
    public enum InputProvider
    {
        Sogou,
        Baidu,
        QQ
    }

    public partial class ChineseInputControl : UserControl
    {
        public event EventHandler Done;

        public InputProvider Provider
        {
            get;
            set;
        }

        public ObservableCollection<string> Candidates;

        private bool isWritting;

        public string Text 
        { 
            get 
            { 
                return InputBox.Text; 
            }

            set
            {
                InputBox.Text = value;
            }
        }

        public ChineseInputControl()
        {
            Provider = InputProvider.Sogou;

            Candidates = new ObservableCollection<string>();

            InitializeComponent();

            CandidateList.ItemsSource = Candidates;
            previewPanel.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void CandidateList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selection = CandidateList.SelectedItem;

            Select(selection as string);
        }

        private void Select(string selection)
        {
            InputBox.Text += selection;

            PreviewBox.Text = string.Empty;
            isWritting = false;
            previewPanel.Visibility = System.Windows.Visibility.Collapsed;

            this.Dispatcher.BeginInvoke(() =>
                {
                    InputBox.Select(InputBox.Text.Length, 0);
                    InputBox.Focus();
                    Candidates.Clear();
                }
            );
        }

        public void StartInput()
        {
            Visibility = System.Windows.Visibility.Visible;
            this.InputBox.Focus();

            isWritting = true;
            previewPanel.Visibility = System.Windows.Visibility.Visible;
        }

        private void InputBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (!isWritting && e.Key == Key.Back)
                return;

            if (e.Key == Key.Back)
            {
                if (!string.IsNullOrEmpty(PreviewBox.Text))
                {
                    PreviewBox.Text = PreviewBox.Text.Substring(0, PreviewBox.Text.Length - 1);
                }
            }
            else if (e.Key == Key.Space)
            {
                if (Candidates.Count > 0)
                {
                    Select(Candidates.First());
                }
            }
            else
            {
                string key = e.Key.ToString();
                PreviewBox.Text += key.ToLower();

                isWritting = true;
                previewPanel.Visibility = System.Windows.Visibility.Visible;
            }

            CloudInputInterface input = GetInputMethod();
            input.ResponseReceived += new EventHandler<CloudInputResponseEventArgs>(input_ResponseReceived);
            input.SendRequest(PreviewBox.Text);

            e.Handled = true;
        }

        private CloudInputInterface GetInputMethod()
        {
            switch (Provider)
            {
                case InputProvider.Baidu:
                    return new BaiduCloudInput();

                case InputProvider.Sogou:
                    return new SogouCloudInput();

                case InputProvider.QQ:
                    break;
            }

            return null;
        }

        void input_ResponseReceived(object sender, CloudInputResponseEventArgs e)
        {
            Candidates.Clear();

            foreach (var item in e.Candidates)
                Candidates.Add(item);
        }

        private void OnPost(object sender, RoutedEventArgs e)
        {
            if (Done != null)
                Done(this, EventArgs.Empty);

            Stop();
        }

        internal void Stop()
        {
            Visibility = System.Windows.Visibility.Collapsed;
            Candidates.Clear();
            PreviewBox.Text = string.Empty;
            InputBox.Text = string.Empty;
        }
    }
}
